package com.czh.springboot04.MyController;


import com.czh.springboot04.entity.Consult;
import com.czh.springboot04.service.ConsultService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/consult")
public class ConsultController {
    @Autowired
    private ConsultService consultService;


    @GetMapping("/getCount")
    public R getCount() {
        List<HashMap<String, Object>> count = consultService.getCount();
        return R.ok(count);
    }
    @PostMapping("/add")
    public R add(@RequestBody Consult consult) {
        //System.out.println(consult);
        int add = consultService.add(consult);
        System.out.println(add);
        if (add > 0) {
            return R.ok(consult);
        }else {
            return R.fail("添加失败");
        }
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id")int id) {
        int delete = consultService.delete(id);
        if (delete > 0) {
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    @PutMapping("/update")
    public R update(@RequestBody Consult consult) {
        System.out.println(consult);
        int update = consultService.edit(consult);
        if (update > 0) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable("id") int id) {
        Consult byId = consultService.findById(id);
        if (byId != null) {
            return R.ok(byId);
        }
        return R.fail("查询失败");
    }

    @GetMapping("/findAllReally")
    public R findAllReally() {
        List<Consult> all = consultService.findAllReally();
        if (all!= null) {
            return R.ok(all);
        }
        return R.fail("查询失败");
    }

    @GetMapping("/findAll")
    public R findAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "10") int pageSize, Consult consult) {
        System.out.println(consult);
        PageInfo<Consult> consults = consultService.findAll(pageNum, pageSize, consult);
        if (consults!= null) {
            return R.ok(consults);
        }
        return R.fail("查询失败");
    }

    @GetMapping("/findConsultByName")
    public R findConsultByName(String name) {
        Consult consult = consultService.findConsultByName(name);
        if (consult!= null) {
            return R.ok(consult);
        }
        return R.fail("查询失败");
    }

    @GetMapping("/findAllByConsult")
    public R findAllByUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",required = false,defaultValue = "6") int pageSize) {
        PageInfo<Consult> consults = consultService.findAllByUserId(pageNum, pageSize);
        if (consults!= null) {
            return R.ok(consults);
        }else {
            return R.fail("查询失败");
        }
    }



}
